<?php use_helper('Pagination') ?>	
<div id="wrapper" class="courses">
	<div id="content">
		<div id="main">
		
		<table>
				<tr>
				<td>
					subject:
					<select id='subjects'>
					<?php foreach($subjects as $subject):?>
						<option value="<?php echo $subject->getId()?>"><?php echo $subject->getName()?></option>
					<?php endforeach?>
					</select>
					<br/>
					<input type="button" onclick="changeSubject()" value="Change Subject"/>
				</td>
				</tr>
		</table>
		
		<br/>
		<br/>
		
		<table>
			<tr style="border-bottom: 2px solid black;">
				<td style="font-weight: bold">
					Course Name
					
				</td>
				<td style="font-weight: bold">
					Course Section
					
				</td>
				<td style="font-weight: bold">
					Course Description
				</td>
				<td style="font-weight: bold">
					Course Subject
				</td>
				<td>
				</td>
			</tr>

			<?php foreach($courses as $index=>$course):?>
				<?php 
					$color = '#ffffff';
					if(($index % 2) == 0)
					{
						$color = 'gray';
					}
				?>
				<tr style="margin-top: 5px;" bgcolor="<?php echo $color?>">
					<td> 
						<input type="text" size="20" value="<?php echo $course->getName()?>" id="course_name_<?php echo $course->getId()?>"/>
					</td>
					<td> 
						<input type="text" size="20" value="<?php echo $course->getSection()?>" id="course_section_<?php echo $course->getId()?>"/>
					</td>
					<td> 
						<textarea rows="3" cols="30"  id="course_desc_<?php echo $course->getId()?>">
						<?php echo $course->getDescription()?>
						</textarea>
					</td>
					<td> 
						<select id="course_sub_<?php echo $course->getId()?>">
						<?php foreach($subjects as $subject):?>
							<?php if($course->getSubject()->getId() == $subject->getId()):?>
						
								<option value="<?php echo $subject->getId()?>" size="10" selected="selected"><?php echo $subject->getName()?></option>
							<?php else:?>
								<option value="<?php echo $subject->getId()?>" size="10"><?php echo $subject->getName()?></option>
							<?php endif?>
						<?php endforeach?>
						</select>
					</td>
					<td>
						<input type="button" value="save" onclick="editCourse(<?php echo $course->getId()?>)"/>
					
					</td>
				</tr>
			<?php endforeach?>


				<?php if($courses_pager->haveToPaginate()): ?>
						<tr>
							<td colspan="5">
					<?php
						$subjectParam = $sf_request->getParameter('subject') ? ("?subject=".$sf_request->getParameter('subject')) : "";
					?>
								<?php echo pager_navigation($courses_pager, 'fixcourses/index'.$subjectParam) ?>
							</td>
						</tr>
				<?php endif?>

			</table>
		</div>
	</div>
</div>
<script>
	function editCourse(id)
	{
		new_name = $('#course_name_'+id).val();
		new_sub = $('#course_sub_'+id).val();
		new_desc = $('#course_desc_'+id).val();
		new_section = $('#course_section_'+id).val();

		
		params = {'id':id, 'name':new_name, 'subject':new_sub, 'description':new_desc, 'section':new_section};
		
		$.ajax({
			type: "POST",
			url: "<?php echo url_for('fixcourses/updateName')?>",
			data: params,
		});

	}

	function changeSubject()
	{
		id = document.getElementById('subjects').value;
		window.location.assign("<?php echo url_for('fixcourses/index').'/index/subject/'?>" + id);

	}
	
</script>